package com.example.userlogin;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBController extends SQLiteOpenHelper {

	private static final String LOGCAT = null;
	SQLiteDatabase database;

	public DBController(Context applicationcontext) {
		//super(context, name, factory, version);
		// TODO Auto-generated constructor stub
		super(applicationcontext, "androidsqlite.db", null, 2);
        Log.d(LOGCAT,"Created database");
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		// TODO Auto-generated method stub
		String query;
		query = "CREATE TABLE userreg (userId INTEGER PRIMARY KEY, userName TEXT, password TEXT, email TEXT)";
		database.execSQL(query);
        Log.d(LOGCAT,"userReg table Created");
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		String query;
		query = "CREATE TABLE userreg ( userId INTEGER PRIMARY KEY, userName TEXT, password TEXT, email TEXT)";
		database.execSQL(query);
        Log.d(LOGCAT,"userReg table Created");
	}
	
	public void insert(HashMap<String, String> queryValues) {
		SQLiteDatabase database = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("userName", queryValues.get("userName"));
		values.put("password", queryValues.get("password"));
		values.put("email", queryValues.get("email"));
		database.insert("userreg", null, values);
		database.close();

		Log.d(LOGCAT,"userReg table inserted");
	}
	
	/*
	
	public int update(HashMap<String, String> queryValues) {
		SQLiteDatabase database = this.getWritableDatabase();	 
	    ContentValues values = new ContentValues();
	    values.put("userName", queryValues.get("userName"));
		values.put("password", queryValues.get("password"));
		return database.update("userlog", values, "userId" + " = ?", new String[] { queryValues.get("userId") });
	}
	
	public void delete(String id) {
		Log.d(LOGCAT,"delete");
		SQLiteDatabase database = this.getWritableDatabase();	 
		String deleteQuery = "DELETE FROM  users where userId='"+ id +"'";
		Log.d("query",deleteQuery);		
		database.execSQL(deleteQuery);
	}

*/
	public ArrayList<HashMap<String, String>> get() {
		ArrayList<HashMap<String, String>> wordList;
		wordList = new ArrayList<HashMap<String, String>>();
		String selectQuery = "SELECT  * FROM userreg";
	    SQLiteDatabase database = this.getWritableDatabase();
	    Cursor cursor = database.rawQuery(selectQuery, null);
	    if (cursor.moveToFirst()) {
	        do {
	        	HashMap<String, String> map = new HashMap<String, String>();
	        	map.put("userName", cursor.getString(1));
	        	map.put("password", cursor.getString(2));
	        	map.put("email", cursor.getString(3));
                wordList.add(map);
	        } while (cursor.moveToNext());
	    }
	    Log.d(LOGCAT,"userReg table get");
	    // return contact list
	    return wordList;
		
	}
	
	public boolean Login(String username, String password) throws SQLException  
    {  
        Cursor mCursor = database.rawQuery("SELECT password FROM userreg WHERE userName=? AND password=?", new String[]{username,password});  
        if (mCursor != null) {  
            if(mCursor.getCount() > 0)  
            {  
                return true;  
            }  
        }  
     return false;  
    }  

}
